package cn.wolfcode.luowowo.website.controller;

import cn.wolfcode.luowowo.article.domain.*;
import cn.wolfcode.luowowo.article.query.StrategyCatalogQuery;
import cn.wolfcode.luowowo.article.query.TravelQuery;
import cn.wolfcode.luowowo.article.service.*;
import cn.wolfcode.luowowo.serach.query.DestTemeSearchQueryObject;
import cn.wolfcode.luowowo.serach.service.IDestinationCommendSearchService;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * Created by admin on 2019/8/10.
 */

@Controller
@RequestMapping("/destination")
public class DestinationControler {

    @Reference
    private IRegionService regionService;

    @Reference
    private IDestinationService destinationService;

    @Reference
    private IStrategyCatalogService strategyCatalogService;

    @Reference
    private IStrategyDetailService strategyDetailService;

    @Reference
    private ITravelService travelService;

    @Reference
    private IStrategyCommendService strategyCommendService;

    @Reference
    private IDestinationCommendSearchService commendSearchService;

    @Reference
    private IDestinationCommendThemeService destinationCommendThemeService;

    @Reference
    private IDestinationCommendService destinationCommendService;

    @RequestMapping("")
    public String Destination(Model model) {
        StrategyDetail strategyDetail = strategyDetailService.selectStrategyDetailTop1();
        model.addAttribute("commend", strategyDetail);
        model.addAttribute("dests",destinationCommendService.selectDestTop5());
        model.addAttribute("hotRegions", regionService.queryHotRegion());

        return "/destination/index";
    }


    @RequestMapping("getHotDestByRegionId")
    public String getHotDestByRegionId(Model model, Long regionId) {
        List<Destination> list = regionService.getDestByRegionId(regionId);

        model.addAttribute("leftDests", list.subList(0, list.size() / 2 + 1));

        model.addAttribute("rightDests", list.subList(list.size() / 2 + 1, list.size()));
        model.addAttribute("regionId", regionId);
        return "/destination/hotdestTpl";
    }

    @RequestMapping("guide")
    public String guide(Model model, Long id) {
        List<Destination> list = destinationService.queryForParent(id);
        Destination dest = list.remove(list.size() - 1);
        model.addAttribute("toasts", list);
        model.addAttribute("dest", dest);

        // catalogs
        List<StrategyCatalog> catalogs = strategyCatalogService.queryCatalogByDestId(id);
        model.addAttribute("catalogs", catalogs);

        // strategyDetails
        List<StrategyDetail> strategyDetails = strategyDetailService.getViewNumTop3(id);
        model.addAttribute("strategyDetails", strategyDetails);
        return "/destination/guide";
    }


    @RequestMapping("surveyPage")
    public String surveyPage(Model model, @ModelAttribute("qo") StrategyCatalogQuery qo) {

        //toasts
        List<Destination> list = destinationService.queryForParent(qo.getDestId());
        Destination dest = list.remove(list.size() - 1);
        model.addAttribute("toasts", list);
        //dest
        model.addAttribute("dest", dest);

        return "/destination/survey";
    }

    @RequestMapping("survey")
    public String survey(Model model, @ModelAttribute("qo") StrategyCatalogQuery qo) {

        // catalogs
        List<StrategyCatalog> catalogs = strategyCatalogService.queryCatalogByDestId(qo.getDestId());
        model.addAttribute("catalogs", catalogs);

        // catalog
        StrategyCatalog catalog = strategyCatalogService.get(qo.getCatalogId());
        model.addAttribute("catalog", catalog);

        // detail
        List<StrategyDetail> details = catalog.getDetails();
        if (details != null && details.size() > 0) {
            StrategyDetail detail = details.get(0);
            detail.setStrategyContent(strategyDetailService.getContent(detail.getId()));
            model.addAttribute("detail", detail);
        }
        return "/destination/surveyTpl";
    }


    @RequestMapping("travels")
    public String travels(Model model, @ModelAttribute("qo") TravelQuery qo) {
        PageInfo list = travelService.query(qo);
        model.addAttribute("pageInfo", list);
        return "destination/travelTpl";
    }


    @RequestMapping("getDestByMonthId")
    public String getDestByMonthId(Model model, int monthId){

        model.addAttribute("monthThemes",commendSearchService.getDestByMonthId(monthId));

        return "destination/recommendTpl";
    }

    @RequestMapping("getThemeById")
    public String getThemeById(Model model,int id){
        model.addAttribute("catemdds" , destinationCommendThemeService.getThemeByType(id));
        return "destination/themeSiftTpl";
    }


    @RequestMapping("destFilter")
    public String travelFilter(Model model ,@ModelAttribute("qo") DestTemeSearchQueryObject qo){
        model.addAttribute("annual", destinationCommendThemeService.getThemeByType(0));
        model.addAttribute("season", destinationCommendThemeService.getThemeByType(1));
        model.addAttribute("goings", destinationCommendThemeService.getThemeByType(2));
        model.addAttribute("festivals", destinationCommendThemeService.getThemeByType(3));
        return "destination/destFilter";
    }

    @RequestMapping("getBourn")
    public String bourn(Model model ,@ModelAttribute("qo") DestTemeSearchQueryObject qo){
        Page list = commendSearchService.query(qo);
        model.addAttribute("page",list);
        return "destination/bourn";
    }
}
